<?php
require_once("NavigateView.php");
class LoginView{
	
	private $username = "username";
	private $password = "password";
	private $loginbutton = "loginbutton";
	private $logoutbutton = "logoutbutton";
	private $checkbox = "remember";
	
	private $SubmitButtonValue = "Login";
	private $LogoutButtonValue = "Logout";
	private $cookieUserName = "CookieUsername";
	private $cookiePassword = "CookiePassword";
	
	private $registrationbutton = "New User";
	private $registrationvalue ="Registratenew";
	
	private $contr = "Controller";

	const USER_NOT_LOGGED_IN = 1;
	const WRONG_USER_OR_PASS = 2;
	const LOGGED_OUT = 3;
	const ISLOGGEDIN_NOT_WORK = 4;
	const DOLOGIN_NOT_WORK = 5;
	const LOGINHANDLER_TEST_PASS = 6;
	const LOGINHANDLER_NOT_PASS = 7;
	const LOGGED_IN = 8;
	
	//Skriver ut en "Login" knapp
	public function DoLoginBox() {
		$navView = new NavigateView();
		return "<div><form method=\"post\" action='?'>
		Username: <input type=\"text\" name=\"username\" /></br>
		Password: <input type=\"password\" name=\"password\" /></br>
		<input type=\"submit\" value=\"$this->SubmitButtonValue\" name=\"loginbutton\" />
		<input type=\"checkbox\" name=\"remember\" value=\"$this->LogoutButtonValue\" /> Remember me</form>
		<br/><a href='?". NavigateView::Cntrller . "=" . NavigateView::RegisterCntrller . "' value='$this->registrationvalue' >$this->registrationbutton</a></div>";
	}
	//Skriver ut en "Logout" knapp
	public function DoLogoutBox() {
		return "<div><form method =\"post\"><input type=\"submit\" value=\"Logout\" name=\"logoutbutton\"/></form></div>";
	}
	//Hämtar användarens användarnamn ifrån textrutan "username" eller cookies.
	public function GetUserName() {
		if($this->DidRememberMe())
		{
			return $_COOKIE[$this->cookieUserName];
		}
		else
		{
			return (trim($_POST[$this->username]) != "" ? $_POST[$this->username] : NULL);
		}
	}
	//Hämtar användarens lösenord ifrån textrutan "password" eller cookies.
	public function GetPassword() {
		if($this->DidRememberMe())
		{
			return $_COOKIE[$this->cookiePassword];
		}
		else
		{
			return (trim($_POST[$this->password]) != ""  ? $_POST[$this->password] : NULL);	
		}
	}
	//Ser om användaren klickat på "login" knappen
	public function TriedToLogIn() {
		if(isset($_POST[$this->loginbutton]) && $_POST[$this->loginbutton] == $this->SubmitButtonValue){
			return true;
		}
		return false;
	}
	//Ser om användaren klickat på "logout" knappen
	public function TriedToLogOut() {
		return (isset($_POST[$this->logoutbutton]) && $_POST[$this->logoutbutton] == $this->LogoutButtonValue);
	}
	//Ser om användaren klickat på "Remember me"
	public function TickedRememberMe() {
		return isset($_POST[$this->checkbox]);
	}
	
	/**
	 * Tar bort cookies
	 */
	public function LogOut(){
		setcookie($this->cookieUserName, "", (time() - 3600));
		unset($_COOKIE[$this->cookieUserName]);
		setcookie($this->cookiePassword, "", (time() - 3600));
		unset($_COOKIE[$this->cookieUserName]);
	}
	/**
	 * Sätter cookies till det angivna användarnamnet och lösenordet.
	 */
	public function RememberMe(){
		
			setcookie($this->cookieUserName, $this->GetUserName(), time() + 30);
			setcookie($this->cookiePassword, sha1($this->GetPassword()), time() + 30);
			$_COOKIE[$this->cookieUserName] = $this->GetUserName();
			$_COOKIE[$this->cookiePassword] = sha1($this->GetPassword());
	}
	
	/** Funktion för att se om cookies existerar eller inte
	 * @return true om cookies är satta, false om cookies inte är satta.
	 */
	public function DidRememberMe(){
		if(isset($_COOKIE[$this->cookieUserName]) && isset($_COOKIE[$this->cookiePassword])){
			return TRUE;
		}
		
		return FALSE;
	}

	/**
	 * Genererar olika meddelanden
	 * @param $index, konstanten som bestämmer vilket meddelande som ska användas
	 * @return String
	 */
	public function Messages($index){
		switch ($index) {
			case self::LOGGED_IN:
				return "</br>" . "User ". $_SESSION['loggedinas'] . " is logged in.";
				break;
			case self::USER_NOT_LOGGED_IN:
				return "You are not logged in!";
				break;
			case self::WRONG_USER_OR_PASS:
				return "Wrong username or password!";
				break;
			case self::LOGGED_OUT:
				return "You have logged out!";
				break;
			case self::ISLOGGEDIN_NOT_WORK:
				return "IsLoggedIn doesn't work!";
				break;
			case self::DOLOGIN_NOT_WORK:
				return "DoLogin doesn't work!";
				break;
			case self::LOGINHANDLER_TEST_PASS:
				return "LoginHandler test passed!";
				break;
			case self::LOGINHANDLER_NOT_PASS:
				return "LoginHandler doesn't work!";
				break;
			default:
				return "Error! This message is not set!";
				break;
		}
	}
	
}

?>
